/**
 ******************************************************************************
 * @file      lib.c
 * @brief     C Source file of lib.c.
 * @details   This file including all API functions's implement of lib.c.
 * @copyright
 ******************************************************************************
 */

/*-----------------------------------------------------------------------------
 Section: Includes
 ----------------------------------------------------------------------------*/
#include <stdio.h>

#include "../GDW1376_2.h"
#include "maths.h"

/*-----------------------------------------------------------------------------
 Section: Type Definitions
 ----------------------------------------------------------------------------*/
/* NONE */

/*-----------------------------------------------------------------------------
 Section: Constant Definitions
 ----------------------------------------------------------------------------*/
/* NONE */

/*-----------------------------------------------------------------------------
 Section: Global Variables
 ----------------------------------------------------------------------------*/
char buf[4096] = {0};

/*-----------------------------------------------------------------------------
 Section: Local Variables
 ----------------------------------------------------------------------------*/
/* NONE */

/*-----------------------------------------------------------------------------
 Section: Local Function Prototypes
 ----------------------------------------------------------------------------*/
/* NONE */

/*-----------------------------------------------------------------------------
 Section: Global Function Prototypes
 ----------------------------------------------------------------------------*/
/* NONE */

/*-----------------------------------------------------------------------------
 Section: Function Definitions
 ----------------------------------------------------------------------------*/
#ifndef BUILD_ARM
/**
 ******************************************************************************
 * @brief   计算累加校验和，累加查出1字节自动溢出
 * @param[in]  pfbuf : 缓冲区
 * @param[in]  len   : 缓冲区长度
 *
 * @retval    返回计算出的校验和
 ******************************************************************************
 */
unsigned char get_cs(const unsigned char *pfbuf, unsigned int len) {
  unsigned char cs = 0u;
  unsigned int i;

  for (i = 0; i < len; i++) {
    cs = cs + pfbuf[i];
  }

  return cs;
}
#endif

/**
 ******************************************************************************
 * @brief   计算fn
 * @param[in]  DT1 :
 * @param[in]  DT2 :
 *
 * @retval  fn
 * @note    暂时只支持1个fn
 ******************************************************************************
 */
int get_fn(unsigned char DT1, unsigned char DT2) {
  int i;

  for (i = 7; i >= 0; i--) {
    if (BITS(DT1, i)) {
      return (DT2 * 8) + i + 1;
    }
  }

  return 0;
}

/**
 ******************************************************************************
 * @brief   计算fn
 * @param[in]  *DT1 :
 * @param[in]  *DT2 :
 *
 * @retval  fn
 * @note    暂时只支持1个fn
 ******************************************************************************
 */
int get_clear_fn(unsigned char *DT1, unsigned char *DT2) {
  int i;

  for (i = 7; i >= 0; i--) {
    if (BITS(*DT1, i)) {
      //            SETBITS(*DT1, i, 0); //清除标志
      return (*DT2 * 8) + i + 1;
    }
  }

  return 0;
}

/**
 ******************************************************************************
 * @brief   输出字符串
 * @param[in]  None
 * @param[out] None
 * @retval     None
 *
 * @details
 *
 * @note
 ******************************************************************************
 */
void pcb_len_err(pcallback pcb, const char *pline_head, const char *pline_end,
                 int len, int checklen) {
  if (len != checklen) {
    sprintf(buf, "%sERROR:报文应用数据长度有误len[%d]checklen[%d]!%s",
            pline_head, len, checklen, pline_end);
    pcb(buf);
  }
}

/*---------------------------------lib.c-------------------------------------*/
